/**
author : 344184416@qq.com
desc : Dirty's js package
**/
var Dir = {
	ajax : function(url,data,Callback){
		$.ajax({
			url : url,
			type : 'post',
			dataType : 'json',
			data : data,
			success : function(res){
				Callback(res);
			}, 
			error : function(){ console.log('ajax error!'); }
		});
	},
	/* 
	popOpen用法:
	Dir.popOpen({
		w:300,	//宽度
		tit:'标题',
		mask:true,	//遮罩
		confirm:true,	//底部确定，取消按扭
		content:'<div>内容</div>',
		sureCallback : function(){},	//确定按扭回调函数
		cancelCallback : function(){}	//取消按扭回调函数
	}); 
	*/
	popOpen : function(obj){
		//get obj
		var tit = '未定义标题',
			w = 200,
			mask = true,
			confirm = true,
			content = '',
			closeBtn = true,
			closeCallback = function(){},
			sureBtn = true,
			sureCallback = function(){Dir.popClose();},
			cancelBtn = true,
			cancelCallback = function(){Dir.popClose();};
		if( obj ){
			if( (typeof obj.tit) != 'undefined' ){ tit = obj.tit }
			if( (typeof obj.w) != 'undefined' ){ w = obj.w }
			if( (typeof obj.mask) != 'undefined' ){ mask = obj.mask }
			if( (typeof obj.confirm) != 'undefined' ){ confirm = obj.confirm }
			if( (typeof obj.content) != 'undefined' ){ content = obj.content }
			if( (typeof obj.closeBtn) != 'undefined' ){ closeBtn = obj.closeBtn }
			if( (typeof obj.closeCallback) != 'undefined' ){ closeCallback = obj.closeCallback }
			if( (typeof obj.sureBtn) != 'undefined' ){ sureBtn = obj.sureBtn }
			if( (typeof obj.sureCallback) != 'undefined' ){ sureCallback = obj.sureCallback }
			if( (typeof obj.cancelBtn) != 'undefined' ){ cancelBtn = obj.cancelBtn }
			if( (typeof obj.cancelCallback) != 'undefined' ){ cancelCallback = obj.cancelCallback }
		}
		
		//get winSize
		var winSize = {
			winWidth : $(window).width(),
			winHeight : $(window).height(),
			docWidth : $(document).width(),
			docHeight : $(document).height()
		}
		//set dom
		var str =   '<div id="pop" style="position:fixed; _position:absolute; z-index:99999; border-radius:2px; border:1px solid #333; left:'+((winSize.winWidth-w-2)/2)+'px; top:0; width:'+w+'px; background:#fff; box-shadow:0px 1px 5px #444;"><div class="popTit" style="position:relative; padding:5px; height:20px; line-height:20px; background:#333; color:#fff;"><span>'+tit+'</span>';
		if( closeBtn ){
			str += '<a class="popX" href="javascript:void(0);" style="position:absolute; right:0; top:0; color:#fff; text-decoration:none; padding:5px;">X</a>';
		}
		str += '</div><div class="popCon" style="padding:5px;">'+content+'</div>';
		if( confirm ){
			str += '<div class="popConfirm" style="overflow:hidden; border-top:1px solid #333;">';
			if( cancelBtn ){
				str += '<a href="javascript:void(0);" class="cancel" style="float:right; height:22px; line-height:22px; _line-height:24px; overflow:hidden; width:60px; text-align:center; background:#333; color:#fff; text-decoration:none; margin:5px;">取消</a>';
			}
			if( sureBtn ){
				str += '<a href="javascript:void(0);" class="sure" style="float:right; height:22px; line-height:22px; _line-height:24px; overflow:hidden; width:60px; text-align:center; background:#333; color:#fff; text-decoration:none; margin:5px;">确定</a>';
			}
			str += '</div>';
		}
		str += '</div>';
		if( mask ){
			str += '<div id="mask" style="position:absolute; z-index:999; left:0; top:0; width:100%; height:'+winSize.docHeight+'px'+'; background:#333; opacity:0.5; filter:alpha(opacity=50);"></div>';
		}
		$('body').append(str);
		//set pop
		var popWidth = $('#pop').width(),
			popHeight = $('#pop').height();
		$('#pop').css({
			top : (winSize.winHeight-popHeight-2)/2 + 'px'
		});
		//win resize
		$(window).resize(function(){
			winSize = {
				winWidth : $(window).width(),
				winHeight : $(window).height(),
				docWidth : $(document).width(),
				docHeight : $(document).height()
			}
			$('#pop').css({
				left : (winSize.winWidth-w-2)/2 + 'px',
				top : (winSize.winHeight-popHeight-2)/2 + 'px'
			})
		});
		//callback
		if( confirm ){
			$('#pop a').click(function(){
				var active = $(this).attr('class');
				if( active == 'sure' && sureBtn ){
					sureCallback();
				}
				if( active == 'cancel' && cancelBtn ){
					cancelCallback();
				}
			});
		}
		//close
		if( closeBtn ){
			$('#pop a.popX').click(function(){
				Dir.popClose(closeCallback);
			});
		}
	},
	popClose : function(Callback){
		$('#pop,#mask').remove();
		if( typeof Callback == 'function' ){
			Callback();
		}
	}
}

